package com.woniuxy.service.impl;

import com.woniuxy.dao.*;
import com.woniuxy.entity.*;
import com.woniuxy.dao.PaperItemDao;
import com.woniuxy.dao.QuestionOptionMapper;
import com.woniuxy.dao.QuestionTopicMapper;
import com.woniuxy.entity.PaperItem;
import com.woniuxy.entity.PaperResult;
import com.woniuxy.entity.QuestionOption;
import com.woniuxy.entity.QuestionTopic;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.PaperItemService;
import com.woniuxy.test;
import com.woniuxy.util.GeneratePaperJSON;
import com.woniuxy.util.PaperJSONToRedis;
import com.woniuxy.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @BelongsProject: Online-education-examination-platform-back
 * @BelongsPackage: com.woniuxy.service.impl
 * @Author: Banol Zi
 * @CreateTime: 2022-06-08  11:14
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class PaperItemServiceImpl implements PaperItemService {

    @Autowired
    QuestionTopicMapper questionTopicMapper;

    @Autowired
    QuestionOptionMapper questionOptionMapper;

    @Autowired
    PaperItemDao paperItemDao;

    @Autowired
    ExamDao examDao;

    @Autowired
    PaperJSONToRedis paperJSONToRedis;

    private List<Integer> topicIds;

    private PaperItem paperItem;

    private PaperResult result;

    private Map<String, Object> topicScore;


    @Override
    public Integer getPostData(Map<String, Object> postMap) {


        PaperItem paperItem = new PaperItem();

        List<Integer> topicIds = new ArrayList<>();
        Map<String, Object> topicScore = new HashMap<>();

        List<Map<String, Object>> topicScoreMap = (List<Map<String, Object>>) postMap.get("tids");
        for (Map<String, Object> map : topicScoreMap) {
            Integer topicId = Integer.parseInt(map.get("ids").toString());
            topicScore.put(map.get("ids").toString(), map.get("score"));
            topicIds.add(topicId);
        }
        paperItem.setTids(topicIds);

        String name = (String) postMap.get("name");
        paperItem.setName(name);

        String type = (String) postMap.get("type");
        paperItem.setType(type);

        String diff = (String) postMap.get("diffculty");
        paperItem.setDiffculty(diff);

        String status = (String) postMap.get("status");
        paperItem.setStatus(status);

        Integer cid = (Integer) postMap.get("cid");
        paperItem.setCid(cid);

        Integer id = (Integer) postMap.get("id");
        paperItem.setId(id);

        this.paperItem = paperItem;

        this.topicIds = topicIds;

        this.topicScore = topicScore;

        if (this.paperItem != null && this.topicIds != null) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public Integer savePaperDetail() {
        for (Integer topicId : this.topicIds) {
            QuestionTopic topic = questionTopicMapper.selectByPrimaryKey(topicId);
            String topicString = topic.getTopic();
            String text = topic.getText();
            Integer teid = topic.getTeid();
            Integer typeId = topic.getTypeid();
            List<QuestionOption> options = questionOptionMapper.findOptByTid(topicId);
            Integer score = Integer.valueOf( this.topicScore.get(topicId.toString()).toString());
            GeneratePaperJSON.put(topicId, topicString, options, text, teid, typeId, score);
        }
        String jsonObject = GeneratePaperJSON.doGenerate();
        this.paperItem.setItem(jsonObject);
        Integer id = paperItemDao.updateJSON(this.paperItem);

        if (id != null) {
            GeneratePaperJSON.clear();
            return id;
        } else {
            GeneratePaperJSON.clear();
            return -1;
        }
    }

    @Override
    public List<PaperItem> findAllP(Map map) {
        return paperItemDao.findAllP(map);
    }

    @Override
    public Integer savePaperToRedis(Integer id) {
        PaperItem paperItem = paperItemDao.findPaperItemById(id);
        String exam_JSON = paperItem.getItem();
        Integer itemId = paperItem.getId();
        paperJSONToRedis.putToRedis(itemId,exam_JSON);
        paperJSONToRedis.test();
        return 1;
    }

    @Override
    public String getRedisExam(Map<String,Object> postMap) {
        Integer exam_id = Integer.parseInt(postMap.get("exam_id").toString());
        String exam_json = paperJSONToRedis.getFromRedis(exam_id);
        return exam_json;
    }

    @Override
    public ResponseEntity deletePbyId(Integer id) {
        int i = paperItemDao.deletePById(id);
        if (i > 0) {
            return ResponseEntity.SUCCESS;
        } else {
            return ResponseEntity.ERROR;
        }
    }

    @Override
    public Boolean addPaper(PaperItem paperItem) {
        return paperItemDao.addPaper(paperItem);
    }
    @Override
    public Boolean changePstatus(PaperItem paperItem) {
        return paperItemDao.changePstatus(paperItem);
    }

    private String getTopic(Integer id) {
        QuestionTopic topic = questionTopicMapper.selectByPrimaryKey(id);
        return topic.getTopic();
    }

    @Override
    public Integer saveResultPaper(Map<String, Object> postMap) {
        PaperResult result = new PaperResult();
        List<Integer> topicIds = (List<Integer>) postMap.get("TopicIds");
        result.setTopics(topicIds);

        Integer uid = (Integer) postMap.get("userId");
        result.setUid(uid);

        Integer classid = (Integer) postMap.get("cid");
        result.setClassid(classid);


        return null;
    }


    @Override
    public PaperItem getPaperById(Integer id) {
        PaperItem paperItem = paperItemDao.findPaperItemById(id);
        if (paperItem != null) {
            return paperItem;
        } else {
            return null;
        }
    }

    @Override
    public List<PaperItem> findPaperItemByCategoryId(int id) {
        return paperItemDao.findPaperItemByCategoryId(id);
    }



}
